<template>
  <div :class="['container', { 'sign-up-mode': isLogin }]">
    <div class="form-container">
      <div class="signin-signup">
        <el-form action="#" class="sign-in-form" ref="loginFormRef" :model="loginForm" :rules="loginRules"
                 @submit.prevent="handleLogin" @keyup.enter="handleLogin">
          <h2 class="title">登录</h2>

          <el-form-item prop="username" class="custom-form-item">
            <div class="input-field">
              <i>
                <font-awesome-icon icon="fa-solid fa-user"/>
              </i>
              <input type="text" placeholder="用户名" v-model="loginForm.username"/>
            </div>
          </el-form-item>

          <el-form-item prop="password" class="custom-form-item">
            <div class="input-field">
              <i>
                <font-awesome-icon icon="fa-solid fa-lock"/>
              </i>
              <input type="password" placeholder="密码" v-model="loginForm.password"/>
            </div>
          </el-form-item>

          <el-form-item prop="code" class="custom-form-item">
            <div class="code-container">
              <verification-code
                  v-model:code="loginForm.code"
                  :captcha-enabled="true"
                  :code-url="codeUrl"
                  @refresh="handleRefresh"
              />
            </div>
          </el-form-item>

          <el-button  type="primary" class="btn solid" @click="handleLogin">立即登录</el-button>
<!--          <p class="social-text">通过其他方式</p>
          <div class="social-media">
            <a href="#" class="social-icon">
              <font-awesome-icon icon="fa-brands fa-qq"/>
            </a>
            <a href="#" class="social-icon">
              <font-awesome-icon icon="fa-brands fa-weixin"/>
            </a>
            <a href="#" class="social-icon">
              <font-awesome-icon icon="fa-brands fa-weibo"/>
            </a>
            <a href="#" class="social-icon">
              <font-awesome-icon icon="fa-brands fa-alipay"/>
            </a>
          </div>-->
        </el-form>

        <!-- 使用注册组件 -->
        <register-form
            :code-url="codeUrl"
            @refresh="handleRefresh"
            @register-success="handleRegisterSuccess"
            ref="registerFormRef"
        />
      </div>
    </div>

    <div class="panels-container">
      <div class="panel left-panel">
        <div class="content">
          <h3>加入我们</h3>
          <p>加入我们，成为本站的一份子。</p>
          <button class="btn transparent" @click="isLogin = !isLogin">
            去注册
          </button>
        </div>
        <img src="@/assets/images/signin.svg" class="image" alt=""/>
      </div>
      <div class="panel right-panel">
        <div class="content">
          <h3>已有帐号？</h3>
          <p>立即登录已有帐号，享受独家权益。</p>
          <button class="btn transparent" @click="isLogin = !isLogin">
            去登录
          </button>
        </div>
        <img src="@/assets/images/signup.svg" class="image" alt=""/>
      </div>
    </div>
  </div>
</template>

<script setup>
import VerificationCode from '@/components/verificationCode.vue';
import RegisterForm from '@/views/register.vue'; // 引入注册组件
import { getCodeImg, login } from '@/api/login';
import router from "@/router/index";
import { ref, watch } from 'vue';
import { ElMessage } from 'element-plus';
import {setToken} from "@/utils/auth";

const isLogin = ref(false);
const registerFormRef = ref(null);

// 登录表单
const loginForm = ref({
  username: "",
  password: "",
  rememberMe: false,
  code: "",
  uuid: ""
});

const loginRules = {
  username: [{required: true, trigger: "blur", message: "请输入您的账号"}],
  password: [{required: true, trigger: "blur", message: "请输入您的密码"}],
  code: [{required: true, trigger: "change", message: "请输入验证码"}]
};

const loginFormRef = ref(null);

// 验证码相关
const codeUrl = ref("");

// 获取验证码
const handleRefresh = async () => {
  try {
    const res = await getCodeImg();
    if (res.captchaEnabled) {
      loginForm.value.uuid = res.uuid;
      // 更新注册组件的UUID
      if (registerFormRef.value) {
        registerFormRef.value.setUUID(res.uuid);
      }
      codeUrl.value = "data:image/gif;base64," + res.img;
    }
  } catch (error) {
    console.error('获取验证码失败:', error);
  }
};

// 注册成功处理
const handleRegisterSuccess = () => {
  isLogin.value = false; // 切换到登录界面
};

// 登录处理
const handleLogin = async () => {
  await loginFormRef.value.validate(async (valid) => {
    if (valid) {
      try {
        const res = await login(
            loginForm.value.username,
            loginForm.value.password,
            loginForm.value.code,
            loginForm.value.uuid
        );
        // 处理登录成功
        if (res.code == 200) {
          ElMessage.success("登录成功");
          setToken(res.token)
          router.push('/index');
        } else {
          ElMessage.error(res.msg);
          // 登录失败后刷新验证码
          handleRefresh();
        }
      } catch (error) {
        console.error('登录失败:', error);
        ElMessage.error("登录请求失败");
        // 登录失败后刷新验证码
        handleRefresh();
      }
    } else {
      console.log('表单验证失败');
      return false;
    }
  });
};

// 监听isLogin，刷新验证码
watch(isLogin, () => {
  handleRefresh();
});

// 初始化获取验证码
handleRefresh();
</script>

<style scoped>
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@200;300;400;500;600;700;800&display=swap');

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body,
input {
  font-family: 'Poppins', sans-serif;
}

.container {
  position: relative;
  width: 100%;
  background-color: #fff;
  min-height: 100vh;
  overflow: hidden;
}

.form-container {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
}

.signin-signup {
  position: absolute;
  top: 50%;
  transform: translate(-50%, -50%);
  left: 75%;
  width: 50%;
  transition: 1s 0.7s ease-in-out;
  display: grid;
  grid-template-columns: 1fr;
  z-index: 5;
}

form {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  padding: 0rem 5rem;
  transition: all 0.2s 0.7s;
  overflow: hidden;
  grid-column: 1 / 2;
  grid-row: 1 / 2;
}

form.sign-up-form {
  opacity: 0;
  z-index: 1;
}

form.sign-in-form {
  z-index: 2;
}

.title {
  font-size: 2.2rem;
  color: #444;
  margin-bottom: 10px;
}

.input-field {
  max-width: 380px;
  width: 100%;
  background-color: #f0f0f0;
  margin: 10px 0;
  height: 55px;
  border-radius: 55px;
  display: grid;
  grid-template-columns: 15% 85%;
  padding: 0 0.4rem;
  position: relative;
}

.input-field i {
  text-align: center;
  line-height: 55px;
  color: #acacac;
  transition: 0.5s;
  font-size: 1.1rem;
}

.input-field input {
  background: none;
  outline: none;
  border: none;
  line-height: 1;
  font-weight: 600;
  font-size: 1.1rem;
  color: #333;
}

.input-field input::placeholder {
  color: #aaa;
  font-weight: 500;
}

.social-text {
  padding: 0.7rem 0;
  font-size: 1rem;
}

.social-media {
  display: flex;
  justify-content: center;
}

.social-icon {
  height: 46px;
  width: 46px;
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 0 0.45rem;
  color: #333;
  border-radius: 50%;
  border: 1px solid #333;
  text-decoration: none;
  font-size: 1.1rem;
  transition: 0.3s;
}

.social-icon:hover {
  color: #4481eb;
  border-color: #4481eb;
}

.btn {
  width: 150px;
  background-color: #5995fd;
  border: none;
  outline: none;
  height: 49px;
  border-radius: 49px;
  color: #fff;
  text-transform: uppercase;
  font-weight: 600;
  margin: 10px 0;
  cursor: pointer;
  transition: 0.5s;
}

.btn:hover {
  background-color: #4d84e2;
}

.panels-container {
  position: absolute;
  height: 100%;
  width: 100%;
  top: 0;
  left: 0;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
}

.container:before {
  content: '';
  position: absolute;
  height: 2000px;
  width: 2000px;
  top: -10%;
  right: 48%;
  transform: translateY(-50%);
  background-image: linear-gradient(-45deg, #4481eb 0%, #04befe 100%);
  transition: 1.8s ease-in-out;
  border-radius: 50%;
  z-index: 6;
}

.image {
  width: 100%;
  transition: transform 1.1s ease-in-out;
  transition-delay: 0.4s;
}

.panel {
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  text-align: center;
  z-index: 6;
}

.left-panel {
  pointer-events: all;
  padding: 3rem 17% 2rem 12%;
}

.right-panel {
  pointer-events: none;
  padding: 3rem 12% 2rem 17%;
}

.panel .content {
  color: #fff;
  transition: transform 0.9s ease-in-out;
  transition-delay: 0.6s;
}

.panel h3 {
  font-weight: 600;
  line-height: 1;
  font-size: 1.5rem;
}

.panel p {
  font-size: 0.95rem;
  padding: 0.7rem 0;
}

.btn.transparent {
  margin: 0;
  background: none;
  border: 2px solid #fff;
  width: 130px;
  height: 41px;
  font-weight: 600;
  font-size: 0.8rem;
}

.right-panel .image,
.right-panel .content {
  transform: translateX(800px);
}

/* ANIMATION */
.container.sign-up-mode:before {
  transform: translate(100%, -50%);
  right: 52%;
}

.container.sign-up-mode .left-panel .image,
.container.sign-up-mode .left-panel .content {
  transform: translateX(-800px);
}

.container.sign-up-mode .signin-signup {
  left: 25%;
}

.container.sign-up-mode form.sign-up-form {
  opacity: 1;
  z-index: 2;
}

.container.sign-up-mode form.sign-in-form {
  opacity: 0;
  z-index: 1;
}

.container.sign-up-mode .right-panel .image,
.container.sign-up-mode .right-panel .content {
  transform: translateX(0%);
}

.container.sign-up-mode .left-panel {
  pointer-events: none;
}

.container.sign-up-mode .right-panel {
  pointer-events: all;
}

/* 自定义表单项样式 */
.custom-form-item {
  width: 100%;
  margin-bottom: 20px;
  position: relative;
}

.code-container {
  width: 100%;
  max-width: 380px;
}

/* 错误信息样式 */
:deep(.el-form-item__error) {
  font-size: 15px;
  position: absolute;
  bottom: -20px;
  left: 50%;
  transform: translateX(-50%);
  text-align: center;
  color: #f56c6c;
}

/* 确保表单项内容居中 */
:deep(.el-form-item__content) {
  display: flex;
  justify-content: center;
  margin: 0 !important;
}

/* 移除Element Plus默认样式 */
:deep(.el-form-item.is-error .el-input__wrapper) {
  box-shadow: none;
}

@media (max-width: 870px) {
  .container {
    min-height: 800px;
    height: 100vh;
  }

  .signin-signup {
    width: 100%;
    top: 95%;
    transform: translate(-50%, -100%);
    transition: 1s 0.8s ease-in-out;
  }

  .signin-signup,
  .container.sign-up-mode .signin-signup {
    left: 50%;
  }

  .panels-container {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr 2fr 1fr;
  }

  .panel {
    flex-direction: row;
    justify-content: space-around;
    align-items: center;
    padding: 2.5rem 8%;
    grid-column: 1 / 2;
  }

  .right-panel {
    grid-row: 3 / 4;
  }

  .left-panel {
    grid-row: 1 / 2;
  }

  .image {
    width: 200px;
    transition: transform 0.9s ease-in-out;
    transition-delay: 0.6s;
  }

  .panel .content {
    padding-right: 15%;
    transition: transform 0.9s ease-in-out;
    transition-delay: 0.8s;
  }

  .panel h3 {
    font-size: 1.2rem;
  }

  .panel p {
    font-size: 0.7rem;
    padding: 0.5rem 0;
  }

  .btn.transparent {
    width: 110px;
    height: 35px;
    font-size: 0.7rem;
  }

  .container:before {
    width: 1500px;
    height: 1500px;
    transform: translateX(-50%);
    left: 30%;
    bottom: 68%;
    right: initial;
    top: initial;
    transition: 2s ease-in-out;
  }

  .container.sign-up-mode:before {
    transform: translate(-50%, 100%);
    bottom: 32%;
    right: initial;
  }

  .container.sign-up-mode .left-panel .image,
  .container.sign-up-mode .left-panel .content {
    transform: translateY(-300px);
  }

  .container.sign-up-mode .right-panel .image,
  .container.sign-up-mode .right-panel .content {
    transform: translateY(0px);
  }

  .right-panel .image,
  .right-panel .content {
    transform: translateY(300px);
  }

  .container.sign-up-mode .signin-signup {
    top: 5%;
    transform: translate(-50%, 0);
  }
}

@media (max-width: 570px) {
  form {
    padding: 0 1.5rem;
  }

  .image {
    display: none;
  }

  .panel .content {
    padding: 0.5rem 1rem;
  }

  .container {
    padding: 1.5rem;
  }

  .container:before {
    bottom: 72%;
    left: 50%;
  }

  .container.sign-up-mode:before {
    bottom: 28%;
    left: 50%;
  }
}
</style>